import numpy as np
import matplotlib.pyplot as plt

N = 100
def Runge(x):
    return 1.0 / (1.0 + x**2)

def func1(x):
    return 0.0384615 + 0.192308*(x + 5) - 0.0384615*(x + 5)*(x) 
    
def func2(x):
    return 0.0384615 + 0.0397878*(x + 5) + 0.061008*(x + 5)*(x + 2.5) - 0.0265252*(x + 5)*(x + 2.5)*(x) + 0.00530504*(x + 5)*(x + 2.5)*(x)*(x - 2.5)
 
def func3(x):
    return 0.0384615 + 0.0264644*(x + 5) + 0.0248454*(x + 5)*(x + 3.33333) + 0.0149446*(x + 5)*(x + 3.33333)*(x + 1.66667) - 0.0131699*(x + 5)*(x + 3.33333)*(x + 1.66667)*(x) + 0.00420316*(x + 5)*(x + 3.33333)*(x + 1.66667)*(x)*(x - 1.66667) - 0.000840633*(x + 5)*(x + 3.33333)*(x + 1.66667)*(x)*(x - 1.66667)*(x - 3.33333)

def func4(x):
    return 0.0384615 + 0.0223428*(x + 5) + 0.013956*(x + 5)*(x + 3.75) + 0.0117043*(x + 5)*(x + 3.75)*(x + 2.5) + 0.000674338*(x + 5)*(x + 3.75)*(x + 2.5)*(x + 1.25) - 0.00489646*(x + 5)*(x + 3.75)*(x + 2.5)*(x + 1.25)*(x) + 0.00243964*(x + 5)*(x + 3.75)*(x + 2.5)*(x + 1.25)*(x)*(x - 1.25) - 0.000687223*(x + 5)*(x + 3.75)*(x + 2.5)*(x + 1.25)*(x)*(x - 1.25)*(x - 2.5) + 0.000137445*(x + 5)*(x + 3.75)*(x + 2.5)*(x + 1.25)*(x)*(x - 1.25)*(x - 2.5)*(x - 3.75)

x = np.linspace(-5, 5, N)
plt.title("Runge pheonomenon")
plt.plot(x, Runge(x), '--', label="f(x)")
plt.plot(x, func1(x), label='n=2')
plt.plot(x, func2(x), label='n=4')
plt.plot(x, func3(x), label='n=6')
plt.plot(x, func4(x), label='n=8')

plt.legend()
plt.savefig("B.png", dpi = 750, bbox_inches = 'tight')
